//
//  CBaseBattleScene.cpp
//  Monster_Martial_Arts_Version_0
//
//  Created by bk on 13/1/5.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "CBattleScene.h"
#include "CBattleManager.h"
#include "CBattleSystem.h"
#include "CInputSystem.h"
#include "CBattleUIHud.h"
#include "CUIManager.h"
#include "CTouchAction.h"

cocos2d::CCScene* CBattleScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    CBattleScene *layer = CBattleScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

CBattleScene::~CBattleScene()
{
    CBattleManager::Release();
    
    CBattleSystem::Release();
    
    CInputSystem::Release();
    
    CUIManager::Release();
    
    m_BackGroundSprite->release();
    
    if (m_TouchAction)
    {
        delete m_TouchAction;
        m_TouchAction = NULL;
    }
}

bool CBattleScene::init()
{
    CBattleManager::Create();
    CBattleManager::Get().RegisterLayer(this);
    
    CBattleSystem::Create();
    
    CInputSystem::Create();
    
    CUIManager::Create();
    
    //add background sprite
    m_BackGroundSprite = CCSprite::create(BACKGROUND_NAME);
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
       
    m_BackGroundSprite->setPosition(ccp(size.width/2,size.height/2));
    
    addChild(m_BackGroundSprite);
    
    //========================================================================== test
    
    CBattleUIHud* Hud = CBattleUIHud::create();
    
    Hud->setPosition(ccp(0, 0));
    
    addChild(Hud);
    
    CBattleUIHud* Hud2 = CBattleUIHud::create();
    
    Hud2->setPosition(ccp(160, 0));
    
    addChild(Hud2);
    
    CBattleUIHud* Hud3 = CBattleUIHud::create();
    
    Hud3->setPosition(ccp(320, 0));
    
    addChild(Hud3);
    
    //=========================================================================
    
    scheduleUpdate();
    
    setTouchEnabled(true);
    
    CBattleSystem::Get().Start();
    
    //========================================================================
    
    m_TouchAction = new CTouchAction();
    
    CInputSystem::Get().AddAction(m_TouchAction);
    
    return true;
}

void CBattleScene::update(float dt)
{
    CInputSystem::Get().Update();
    
    CBattleSystem::Get().Update(dt);
}

void CBattleScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    if (m_TouchAction)
    {
        CCSetIterator it = pTouches->begin();
        CCTouch* touch = (CCTouch*)(*it);
        
        CCPoint location = touch->getLocation();
        fPoint touchPoint(location.x, location.y);
        
        m_TouchAction->UpdateTouchData(touchPoint,TOUCH_DATA::TOUCH_TYPE_END);
    }
}

